package com.mstore.service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import com.mstore.dao.UserDao;
import com.mstore.pojo.User;

public class UserService {

	private UserDao userDao;

	/**
	 * 
	 * 添加分店管理员
	 */
	public void addUser(User user) {
		userDao.save(user);
	}

	/**
	 * 
	 * 删除分店管理员信息
	 */
	public void deleteUser(User user) {
		userDao.delete(user);
	}
	
	public void update(User user){
		userDao.updateUser(user);
	}
	

	/**
	 * 更新分店管理员信息
	 */
	public void updateUser(User user) {
		userDao.update(user);
	}

	/**
	 * 
	 * 获取单个分店信息
	 */
	public User getOneUser(User user) {
		User us = (User) userDao.getOneUser(user);
		if (us == null) {
			return null;
		}
		us.setCreateTime(us.getCreateTime().substring(0,
				us.getCreateTime().indexOf(".")));
		return us;
	}
	
	/**
	 * 根据id获取分店信息并抓取member
	 */
	public User getOneUserAndMember(User user) {
		Object object = userDao.getOneUserAndMember(user);
		if (object == null) {
			return null;
		}
		Object[] objects = (Object[])object;
		return (User)objects[0];
	}

	/**
	 * 
	 * 验证分店登陆
	 */
	public Object findUser(User user) {
		return userDao.findUser(user);
	}

	/**
	 * 
	 * 查询企业下的所有用户
	 */
	public List<User> getAllUser(User user) {
		List<Object> objects = userDao.getAllUser(user);
		List<User> users = new ArrayList<User>();
		for (Object object : objects) {
			User us = (User) object;
			us.setCreateTime(us.getCreateTime().substring(0,
					us.getCreateTime().indexOf(".")));
			users.add(us);
		}
		return users;
	}

	/**
	 * 修改密码
	 */
	public void updatePasswordById(User user) {
		userDao.updatePasswordById(user);
	}
	
	/**
	 * 查找除自己以外相同手机号的member的个数
	 */
	public Long getCountMemberByMobileOutSelf(User user) {
		return userDao.getCountMemberByMobileOutSelf(user);
	}

	public void updateParamKey(User user) {
		userDao.updateParamKey(user);
	}

	public boolean checkLoginName(User user) {
		return userDao.checkLoginName(user);
	}

	public boolean checkMobile(User user) {
		return userDao.checkMobile(user);
	}

	public void updateMessage(User user) {
		userDao.updateMessage(user);
	}

	public void updateMessagePassword(User user) {
		userDao.updateMessagePassword(user);
	}

	/**
	 * 根据member_id查找
	 */
	public User findUserByMemberId(User user) {
		Object object = userDao.findUserByMemberId(user);
		return object != null ? (User) object : null;
	}
	
	/**
	 * 查出全部User
	 */
	public List<User> getAllUser1() {
		List<User> users = new ArrayList<User>();
		List<Object> objects = userDao.getAllUser1();
		for (Object object : objects) {
			users.add((User)object);
		}
		return users;
	}
	
	/**
	 * 查找member_id
	 */
	public Long getMemberIdFromUser(Long id){
		Object obj = userDao.getMemberIdFromUser(id);
		@SuppressWarnings("unchecked")
		ArrayList<Object> list = (ArrayList<Object>)obj;
		if (list != null && list.size() > 0) {
			return ((BigInteger)list.get(0)).longValue();
		}
		return null;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
}
